$.ready((function () {
    $("#entry_date").datepicker({ dateFormat: "yy-mm-dd" });
    // 手机号码验证
    jQuery.validator.addMethod("isMobile", function(value, element) {
        var length = value.length;
        var mobile =  /^1(3|5|8|4)\d{9}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写您的手机号码");
    jQuery.validator.addMethod("isIdCardNo", function(value, element) {
        var idcard_l = document.getElementById("identity").checked;
        var passport = document.getElementById("passport").checked;
        $("#lab_exist_id").remove();
        if(idcard_l){
            var result = checkIDCard(value);
            if(result){
                var type = $("#identity").val();
                var number = value;
                jQuery.ajax({
                    async:true,
                    url:url_identity,
                    data:{'card_type':type,'id_card':number},
                    type:'post',
                    success:function (data) {
                        var data = eval('(' + data + ')');
                        if(data.errcode){//存在员工档案
                            $("#lab_exist_id").remove();
                            var obj = $("input[name='identity_code']");
                            var html = '<label id="lab_exist_id" style="font-size: 12px;display: inline;color: red;"><a target="_blank" style="padding-left: 20px" href="'+data.data+'">查看</a></label>';
                            $(html).insertAfter(obj);
                            return;
                        }
                        $("#lab_exist_id").remove();

                        return;

                    }
                });
            }


            return this.optional(element) || result;
        }
        if(passport){
            //alert("paass");
            var result = true;
            return this.optional(element) || result;
        }

    }, "请正确输入您的身份证号码");

    $('#form1').validate({

        rules: {
            real_name: {
                required:true,
                rangelength:[1,15]
            },
            english_name: {
                rangelength:[0,10]
            },
            sex: {
                required:true
            },
            cellphone: {
                required:true,
                isMobile:true
            },
            identity_type: {
                required:true
            },
            identity_code: {
                required:true,
                rangelength:[1,30],
                isIdCardNo:true
            },
            entry_date: {
                required:true
            },
            city_id:{
                required:true
            },
            user_type:{
                required:true
            },
            job_name:{
                required:true
            },
            parent_name:{
                required:true
            }





        },
        messages:{
            real_name: {
                required: "请填写姓名",
                rangelength: "请输入15个以内的字符"
            },
            english_name: {
                rangelength: "请输入10个以内的字符"
            },
            sex: {
                required:"请选择性别"
            },
            cellphone: {
                required:"请输入手机号码",
                isMobile:"请输入正确的手机号码"
            },
            identity_type: {
                required:"请选择证件类型"
            },
            identity_code: {
                required:"请输入证件号码",
                rangelength:"请输入30个以内的字符",
                isIdCardNo:"请输入正确的证件号码"
            },
            entry_date: {
                required:"请输入报到日期",
                date:"请选择一个有效的日期"
            },
            city_id:{
                required:"请选择合同签约城市"
            },
            user_type:{
                required:"请选择员工类型"
            },
            job_name:{
                required:"请选中己存在的职位"
            },
            parent_name:{
                required:"请选中直属上级"
            }

        },
        errorPlacement:function (error, element) {

               // element.style.textAlign="left";
                element.parent().append(error);

        }

    });
    jobResult = false;
    positionResult = false;
    $("input[name='parent_name']").autocomplete({
        source: function(request, response) {

            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        //                    response( $.map( testdata, function( item ) {
                        return {
                            label:  item.name ,
                            value:  item.name,
                            job_name: item.job_name,
                            user_id:  item.user_id,
                            job_id :item.job_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var user_id = ui.item.user_id;
            var job_id = ui.item.job_id;
            ui.item && $(".leader_id").val(user_id);
            ui.item && $(".leader_job").val(job_id);
            positionResult = true;

        }
    });
    $("input[name='parent_name']").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });
    $("input[name='parent_name']").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(".leader_job").val("");
            $(this).val("");

        }
    });
    $("input[name='job_name']").autocomplete({
        source: function(request, response) {
            var ajaxUrl2 = url_job;
             var packcity = $(".city_id").find("option:selected").val();//city
            $.ajax({
                url:ajaxUrl2,
                dataType: "json",
                data: {
                    city:packcity,
                    name: request.term

                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        //response( $.map( testdata, function( item ) {
                        return {
                            label:  item.job_name,
                            value:  item.job_name ,
                            id:  item.job_id,
                            cityID:item.city_id,
                            cityName:item.city_name
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var id = ui.item.id;
            var city_id =ui.item.cityID;
            $(".position_id").val(id);
            $(".position_city").val(city_id);
            jobResult = true;

        }
    });

})());
$("input[name='job_name']").bind("keydown",function(event){
    if((event.keyCode == 13 && jobResult == true)||(event.keyCode == 9 && jobResult == true) || (event.keyCode == 32 && jobResult == true)){
            return;
    }
    jobResult = false;
});
$("input[name='job_name']").bind("blur",function(){
    if(!jobResult){
        $(".position_id").val("");
        $(".position_city").val("");
        $(this).val("");
    }
});
function checkIDCard(id) {
    var idNum = id;
    var errors = new Array(
        "验证通过",
        "身份证号码位数不对",
        "身份证含有非法字符",
        "身份证号码校验错误",
        "身份证地区非法"
    );
    //身份号码位数及格式检验
    var re;
    var len = idNum.length;
    //身份证位数检验
    if (len != 15 && len != 18) {
        return false;
        return errors[1];
    } else if (len == 15) {
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    } else {
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})([0-9xX])$/);
    }

    var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西",
        15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海",
        32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西",
        37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东",
        45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州",
        53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海",
        64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门",
        91: "国外"
    }

    var idcard_array = new Array();
    idcard_array = idNum.split("");

    //地区检验
    if (area[parseInt(idNum.substr(0, 2))] == null) {
        return false;
        return errors[4];
    }

    //出生日期正确性检验
    var a = idNum.match(re);

    if (a != null) {
        if (len == 15) {
            var DD = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var flag = DD.getYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[5];
        }
        else if (len == 18) {
            var DD = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var flag = DD.getFullYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[5];
        }
        if (!flag) {
            //return false;
            return false;
            return "身份证出生日期不对！";
        }
        //检验校验位
        if (len == 18) {
            S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                + parseInt(idcard_array[7]) * 1
                + parseInt(idcard_array[8]) * 6
                + parseInt(idcard_array[9]) * 3;

            Y = S % 11;
            M = "F";
            JYM = "10X98765432";
            M = JYM.substr(Y, 1); //判断校验位

            //检测ID的校验位
            if (M == idcard_array[17]) {
                return true;
                //return "";
            }
            else {
                //return false;
                return false;
                return errors[3];
            }
        }

    } else {
        //return false;
        return false;
        return errors[2];
    }
    return true;
}
//身份证验证
function identityValidate(){
    var obj =$(this);
    var type = $("input[name='unique_type']:checked").val();
    var number = $("input[name='isIdCardNo']").val();
    jQuery.ajax({
        async:true,
        url:url_identity,
        data:{'card_type':type,'id_card':number},
        type:'post',
        success:function (data) {
            var data = eval('(' + data + ')');
            if(data.errcode){//存在员工档案
                $("#lab_exist_id").remove();
                var html = '<label id="lab_exist_id" style="font-size: 12px;display: inline;color: red;">已存在该员工档案<a target="_blank" style="padding-left: 20px" href="'+data.data+'">查看</a></label>';
                $(html).insertAfter(obj);
                return;
            }
            $("#lab_exist_id").remove();
            result = false
            return;

        }
    });

}
$("#save_a").bind('click',function(){
    $("#hid_submit").val("2");
    $("#form1").submit();
});
$("#save_b").bind('click',function(){
    $("#hid_submit").val("3");
    $("#form1").submit();
});
$(".submit").bind('click',function(){
    $("#hid_submit").val("1");
    $("#form1").submit();

});
if($("#msg").length === 1){
   setTimeout(function(){$("#msg").remove();},5000);
}
